package control

import (
	"dicomtodb/models"
	"time"
)

//最后的数据校验
func CheckData() {
	errFile := models.CountrastFile()
	countErr := len(errFile)
	successData := models.CountDiomFromDB()
	if countErr == 0 {
		models.Log.INFO("所有数据处理完成～！！共计:%d", successData)
	} else {
		models.Log.INFO("本次成功数据共计:%d", successData)
		var outMsg []string
		for _, file := range errFile {
			outMsg = append(outMsg, file.FileName)
		}
		models.Log.INFO("异常数据共计:%d", countErr)
		models.Log.INFO("异常文件列表: %v", outMsg)
	}

}

//文件写入过程信息统计
// func FileToDbProgress() {
// 	defer models.Wait.Done()
// 	count := new(models.FileInfo)
// 	for {
// 		c := count.Count()
// 		progress := float64(c) / float64(models.DicomFileCount) * 100
// 		if progress >= 99 {
// 			break
// 		} else {
// 			models.Log.INFO("当前文件写入数据数量%d,写入进度%f %", c, progress)
// 		}
// 		time.Sleep(10 * time.Second)
// 	}
// }

//dicom处理信息统计
func DicomToDbProgress() {
	defer models.Wait.Done()
	for {
		c := models.CountDiomFromDB()
		progress := float64(c) / float64(models.DicomFileCount) * 100
		if progress >= 99 {
			break
		} else if progress == 0 {
			continue
		} else {
			models.Log.INFO("当前处理进度%2.2f%%，已经处理DICOM共%d个", progress, c)
		}
		time.Sleep(60 * time.Second)
	}
}

//文件处理信息统计
func FileToDbProgress() {
	defer models.Wait.Done()
loop:
	for {
		select {
		case msg, ok := <-successMsg:
			if !ok {
				models.Log.INFO("文件处理完成～！！")
				break loop
			}
			models.Log.INFO("当前写入数据库文件名,共计：%d", msg)
		default:
			time.Sleep(10 * time.Second)
			models.Log.INFO("当前写入数据库文件名,共计：%d", models.CountFileFromDB())
		}
	}
}
